package com.xge.component_caipu

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.lifecycle.lifecycleScope
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.google.gson.Gson
import com.xge.common_libary.bean.CaiPuListInfoSection
import com.xge.common_libary.bean.CollectCpEntity
import com.xge.common_libary.bean.Material
import com.xge.common_libary.bean.Proces
import com.xge.component_caipu.binder.ItemBuZhouContentBinder
import com.xge.component_caipu.binder.ItemCaiLiaoContentBinder
import com.xge.component_caipu.room.AppDatabase
import kotlinx.coroutines.launch
import me.drakeet.multitype.MultiTypeAdapter
import om.xge.component_caipu.R
import om.xge.component_caipu.databinding.ActivityCaipuDetailBinding

class CaiPuDetailActivity : AppCompatActivity() {
    private lateinit var binding: ActivityCaipuDetailBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityCaipuDetailBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val db = AppDatabase.getInstance(this)
        val collectDao = db.collectDao()

        val section = intent.getParcelableExtra<CaiPuListInfoSection>("cp_info")

        initData(section)

        binding.clToolbar.getBackByView().setOnClickListener { finish() }

        lifecycleScope.launch {
            val bean = collectDao.getById(section?.id!!)
            if (bean == null) {
                binding.ivCollect.setImageResource(R.mipmap.ic_collect_false)
            } else {
                binding.ivCollect.setImageResource(R.mipmap.ic_collect_true)
            }
        }

        binding.ivCollect.setOnClickListener {
            val currentDrawable = binding.ivCollect.drawable
            val trueDrawable = ContextCompat.getDrawable(this, R.mipmap.ic_collect_true)
            val falseDrawable = ContextCompat.getDrawable(this, R.mipmap.ic_collect_false)

            when (currentDrawable?.constantState) {
                trueDrawable?.constantState -> {
                    // 当前是 ic_collect_true
                    lifecycleScope.launch {
                        collectDao.deleteById(section?.id!!)
                        ToastUtils.showShort("取消收藏成功")
                        binding.ivCollect.setImageResource(R.mipmap.ic_collect_false)
                    }
                }
                falseDrawable?.constantState -> {
                    // 当前是 ic_collect_false
                    lifecycleScope.launch {
                        val json = Gson().toJson(section)
                        val bean = CollectCpEntity(sid = section?.id.toString(), content = json)
                        collectDao.insert(bean)
                        ToastUtils.showShort("收藏成功")
                        binding.ivCollect.setImageResource(R.mipmap.ic_collect_true)
                    }
                }
            }
        }
    }

    private fun initData(section: CaiPuListInfoSection?) {
        binding.clToolbar.setTitleByVal(section?.name?: "")
        binding.tvName.text = section?.name
        binding.tvPeoplenum.text = section?.peoplenum
        binding.tvPreparetime.text = section?.preparetime
        binding.tvCookingtime.text = section?.cookingtime
        binding.tvTag.text = section?.tag
        Glide.with(this).load(section?.pic).into(binding.ivCover)

        section?.material?.let {
            val clAdapter = MultiTypeAdapter(it)
            clAdapter.register(Material::class.java, ItemCaiLiaoContentBinder())
            binding.rvCaiLiao.adapter = clAdapter
        }

        section?.process?.let {
            val clAdapter = MultiTypeAdapter(it)
            clAdapter.register(Proces::class.java, ItemBuZhouContentBinder())
            binding.rvBuzhou.adapter = clAdapter
        }

    }

    companion object {
        fun start(context: Context, section: CaiPuListInfoSection) {
            val intent = Intent(context, CaiPuDetailActivity::class.java)
            intent.putExtra("cp_info", section)
            context.startActivity(intent)
        }
    }
}